package com.eurotalkweather;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;





public class SAXHandlerWeather extends DefaultHandler {
    
    private boolean endDocument = true; //Some times endDocument() is not called, or it is called before 
    					//endDocument() is called, and I do not understand what this his 
    					//happening because they are in the same thread.
    					//With this "endDocument" variable we assure it is called.
    private String dateRaw;
    private String[][] weatherList;
    private int nPeriod = 0, nRep = 0;
    
    
    
    
    
    SAXHandlerWeather() {
	weatherList = new String[10][8];
    }
    
    
    
    
    
    @Override
    public void error(SAXParseException e) {
        System.out.println("Error: " + e.getMessage());
    }
    
    
    
    
    
    @Override
    public void warning(SAXParseException e) {
        System.out.println("Warning: " + e.getMessage());
    }
    
    
    
    
    
    @Override
    public void startElement(String uri, String qName, String elementName,
	    Attributes attributesList) throws SAXException {
	
	if (elementName.equals("Period")) {
	    dateRaw = attributesList.getValue(0); // Date
	    weatherList[nPeriod*2][0] = dateRaw.substring(5, 7); //Month
	    weatherList[nPeriod*2][1] = dateRaw.substring(8, 10); //Day
	    weatherList[nPeriod*2+1][0] = dateRaw.substring(5, 7); //Month
	    weatherList[nPeriod*2+1][1] = dateRaw.substring(8, 10); //Day
	    nPeriod++;
	}
	
	if (elementName.equals("Rep") && nRep < 10) {
	    weatherList[nRep][2] = attributesList.getValue("W"); // Weather type
	    weatherList[nRep][3] = attributesList.getValue("FDm"); // Feels Like Day Maximum Temperature
	    weatherList[nRep][4] = attributesList.getValue("FNm"); // Feels Like Night Maximum Temperature
	    weatherList[nRep][5] = attributesList.getValue("PPd"); // Precipitation Probability Day (this value is not shown)
	    weatherList[nRep][6] = attributesList.getValue("PPn"); // Precipitation Probability Night (this value is not shown)
	    weatherList[nRep][7] = attributesList.getValue("S"); // Wind Speed   
	    nRep++;
	}
    }
    
    
    
    
    
    @Override
    public void endDocument() throws SAXException {
	if (endDocument){
	    for (int n=0; n<10; n++) {
		switch (Integer.parseInt(weatherList[n][0])) {
		case 1: weatherList[n][0] = "Jan"; break;
		case 2: weatherList[n][0] = "Feb"; break;
		case 3: weatherList[n][0] = "Mar"; break;
		case 4: weatherList[n][0] = "Apr"; break;
		case 5: weatherList[n][0] = "May"; break;
		case 6: weatherList[n][0] = "Jun"; break;
		case 7: weatherList[n][0] = "Jul"; break;
		case 8: weatherList[n][0] = "Aug"; break;
		case 9: weatherList[n][0] = "Sep"; break;
		case 10: weatherList[n][0] = "Oct"; break;
		case 11: weatherList[n][0] = "Nov"; break;
		case 12: weatherList[n][0] = "Dic";
		}
	    }
	    endDocument = false;
	}
    }
    
    
    
    
    
    public String[][] getWeather() {
	if (endDocument)
	    try {
		endDocument();
	    } catch (SAXException e) {
		e.printStackTrace();
	    }
	return weatherList;
    }
}